package com.oxygen.pt.utils;
import com.oxygen.pt.pojo.entity.DateScope;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAdjusters;

public class DateRangeUtils {

    // 本周范围（周一至周日）
    public static DateScope getThisWeekRange() {
        LocalDate today = LocalDate.now();
        LocalDate start = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate end = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return new DateScope(start, end);
    }
    // 上周范围 (周一至周日)
    public static DateScope getLastWeekRange() {
        LocalDate today = LocalDate.now();
        LocalDate lastWeekDate = today.minusWeeks(1);
        LocalDate start = lastWeekDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate end = lastWeekDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return new DateScope(start, end);
    }
    // 本月范围
    public static DateScope getThisMonthRange() {
        LocalDate today = LocalDate.now();
        LocalDate start = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate end = today.with(TemporalAdjusters.lastDayOfMonth());
        return new DateScope(start, end);
    }
    //上月范围
    public static DateScope getLastMonthRange() {
        LocalDate today = LocalDate.now();
        LocalDate lastMonthDate = today.minusMonths(1);
        LocalDate start = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate end = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        return new DateScope(start, end);
    }
    // 本年范围
    public static DateScope getThisYearRange() {
        LocalDate today = LocalDate.now();
        LocalDate start = today.with(TemporalAdjusters.firstDayOfYear());
        LocalDate end = today.with(TemporalAdjusters.lastDayOfYear());
        return new DateScope(start, end);
    }
    public static DateScope getLastYearRange() {
        LocalDate lastYearDate = LocalDate.now().minusYears(1);
        LocalDate start = lastYearDate.with(TemporalAdjusters.firstDayOfYear());
        LocalDate end = lastYearDate.with(TemporalAdjusters.lastDayOfYear());
        return new DateScope(start, end);
    }
    // 本季度范围
    public static DateScope getThisQuarterRange() {
        LocalDate today = LocalDate.now();
        int quarter = today.get(IsoFields.QUARTER_OF_YEAR); // 直接获取季度(1-4)

        Month startMonth = Month.of((quarter - 1) * 3 + 1); // 季度起始月份
        LocalDate start = Year.now().atMonth(startMonth).atDay(1);

        Month endMonth = startMonth.plus(2); // 季度结束月份
        LocalDate end = Year.now().atMonth(endMonth).atEndOfMonth();

        return new DateScope(start, end);
    }
    public static DateScope getLastQuarterRange() {
        LocalDate today = LocalDate.now();
        // 通过减3个月计算上一季度日期基点
        LocalDate lastQuarterBaseDate = today.minusMonths(3);
        // 基于新基点重新获取季度
        int quarter = lastQuarterBaseDate.get(IsoFields.QUARTER_OF_YEAR);
        Year year = Year.of(lastQuarterBaseDate.getYear());

        Month startMonth = Month.of((quarter - 1) * 3 + 1);
        LocalDate start = year.atMonth(startMonth).atDay(1);
        Month endMonth = startMonth.plus(2);
        LocalDate end = year.atMonth(endMonth).atEndOfMonth();

        return new DateScope(start, end);
    }
}
